package com.itgungnir.diyshuo.fragment.fenlei_items_fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.itgungnir.diyshuo.activity.R;
import com.itgungnir.diyshuo.base.BaseFragment;

/**
 * 分类页面中对应“单品”的Fragment
 */
public class Fenlei_DanpinFragment extends BaseFragment implements FenleiItemContract.View {
    private TextView title;
    private GridView gv;

    private final int[] DANPIN_GRID_IMAGES = {R.mipmap.icon_fenlei_type01, R.mipmap.icon_fenlei_type03, R.mipmap.icon_fenlei_type06,
            R.mipmap.icon_fenlei_type07, R.mipmap.icon_fenlei_type08, R.mipmap.icon_fenlei_type09, R.mipmap.icon_fenlei_type10,
            R.mipmap.icon_fenlei_type05, R.mipmap.icon_fenlei_type11, R.mipmap.icon_fenlei_type02};
    private final String[] DANPIN_GRID_TITLES = {"穿搭", "居家", "饰品", "鞋子", "美护", "美食家", "科技", "礼物", "创意", "原创", "包包"};

    private FenleiItemPresenter presenter = new FenleiItemPresenter(this);

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        title = (TextView) view.findViewById(R.id.find_fenleimainitem_tv_title);
        gv = (GridView) view.findViewById(R.id.find_fenleimainitem_gv_gv);
        initViews();
        initEvents();
    }

    /**
     * 初始化布局控件
     */
    private void initViews() {
        // 初始化TextView中的数据
        setTextToTextView("单品分类");
        // 初始化GridView中的数据
        setContentToGridView(DANPIN_GRID_IMAGES, DANPIN_GRID_TITLES);
    }

    /**
     * 初始化控件的交互事件
     */
    private void initEvents() {
        // 当点击GridView中的某一项时触发的事件
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                onGridViewItemClickListener(position);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_fenlei_item;
    }

    @Override
    public void setTextToTextView(String text) {
        presenter.setTextToTextView(title, text);
    }

    @Override
    public void setContentToGridView(int[] images, String[] titles) {
        presenter.setContentToGridView(getActivity(), gv, images, titles);
    }

    @Override
    public void onGridViewItemClickListener(int position) {
        presenter.onGridViewItemClickListener(getActivity(), position, DANPIN_GRID_TITLES[position], false);
    }
}